<?php

use Doraemon\model\Comment;
use Doraemon\model\CommentLike;
use Doraemon\model\CommentMasterStat;
use Doraemon\service\User;
use Doraemon\tools\HhzEnv;

class BarrageV2Action extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws Exception
     */
    public function main()
    {
        if (HhzEnv::isShopTest()) {
            $this->uid = 1582647;
            $objId = '00054yz000000hm5';//开发环境
        } else if (HhzEnv::isTest()) {
            $objId = '0009vyc0000bolvm';//好好测
        } else if (HhzEnv::isGray()) {
            $objId = '005y49f00007uw3u';//灰度
        } else {
            $objId = '0061lan00007ez56';//线上
        }
        $commentsStatList = CommentMasterStat::select(['origin_id', 'like_num'], [
            'obj_id' => $objId,
            'like_num[>]' => 0,
            'status' => CommentMasterStat::STATUS_NORMAL,
            'ORDER' => ['like_num' => 'DESC']
        ]);
        $commentsIds = array_column($commentsStatList, 'origin_id');
        $commentsStatList = array_column($commentsStatList, null, 'origin_id');
        $commentList = $commentsIds ? Comment::select('*', ['id' => $commentsIds, 'obj_id' => $objId, 'status' => Comment::STATUS_NORMAL]) : [];
        $uids = array_column($commentList, 'uid');
        $uids = array_unique($uids);
        $userList = $uids ? User::getBatchBasicUserInfo($uids) : [];
        $userList = $userList ? array_column($userList, null, 'uid') : [];
        $commentList = array_column($commentList, null, 'id');
        $data = ['list' => [], 'user_info' => null];
        $userInfo = $this->uid ? User::getBasicUserInfo($this->uid) : [];
        if ($userInfo) {
            $data['user_info'] = [
                'uid' => $userInfo['uid'],
                'nick' => $userInfo['nick'],
                'avatar' => $userInfo['avatar']
            ];
        }
        //$isLikeRes = CommentLike::is_liked_multi($this->uid, $objId, CommentsCounter::LIKE_TYPE_COMMENTS_PHOTO, $commentsIds);
        $commentLike = CommentLike::select(['comments_id'], [
            'uid' => $this->uid,
            'status' => CommentLike::STATUS_NORMAL,
            'obj_id' => $objId,
            'comments_id' => $commentsIds
        ]);
        $isLikeRes = [];
        foreach ($commentLike as $v) {
            $isLikeRes[$v['comments_id']] = 1;
        }
        foreach ($commentsIds as $id) {
            if (isset($commentList[$id]) && $commentList[$id]['content'] && $commentList[$id]['uid']) {
                $data['list'][] = [
                    'id' => $id,
                    'obj_id' => $objId,
                    'content' => $commentList[$id]['content'],
                    'uid' => $commentList[$id]['uid'],
                    'nick' => $userList[$commentList[$id]['uid']]['nick'],
                    'avater' => $userList[$commentList[$id]['uid']]['small_avatar'],
                    'is_like' => (isset($isLikeRes[$id]) && $isLikeRes[$id]) ? $isLikeRes[$id] : 0,
                    'like_num' => $commentsStatList[$id]['like_num'] ?? 0,
                ];
            }
        }
        $this->setSuccess($data);
    }


}